package com.app.scale.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.scale.basis.Constants;
import com.app.scale.greendao.GreenDaoUtils;

import my.FileUtils;
import my.LogUtil;
import my.TimeUtils;

public class BaseInfoUpdateCache extends BasisBean {

    private boolean needUpdateJijiayuan = true;    //计价员
    private boolean needUpdateSupplier = true;        //供应商


    private int jijiayuanCount;
    private int supplierCount;



    private int jijiayuanCountUpdated;
    private int supplierCountUpdated;


    private boolean isForceUpdate = false;


    @JSONField(serialize = false)
    public boolean isNeedUpdate(){
        if(isForceUpdate )return true;
        LogUtil.i("-----time111---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        LogUtil.i("ContactListBean.isCacheExist()---?"+ContactListBean.isCacheExist());

        if( needUpdateJijiayuan ){
            needUpdateJijiayuan = (jijiayuanCount > 0);
        }

        if( needUpdateSupplier ){
            needUpdateSupplier = (supplierCount > 0);
        }

        LogUtil.i("-----time222---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        if( needUpdateJijiayuan||needUpdateSupplier)return true;

            return false;
    }

    @JSONField(serialize = false)
    public void setUpdateCountByType(String map_type,int  count){
        switch (map_type){
            case GreenDaoUtils.TYPE_JI_JIA_YUAN:
                jijiayuanCount = count;
                break;
            case GreenDaoUtils.TYPE_SUPPLIER:
                supplierCount = count;
                break;
        }
    }

    @JSONField(serialize = false)
    public boolean isUpdateSuccess(){
        if( (jijiayuanCountUpdated == 0) && (supplierCountUpdated == 0)){
            return false;
        }
        return true;
    }


    @JSONField(serialize = false)
    public void setUpdatedCountByMaptype(String type,int updatedCount){
        switch (type){
            case GreenDaoUtils.TYPE_JI_JIA_YUAN:
                jijiayuanCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_SUPPLIER:
                supplierCountUpdated = updatedCount;
                break;

        }
    }


    @JSONField(serialize = false)
    public void setAllNeedUpdate(boolean isNeed){
        needUpdateJijiayuan = isNeed;
        needUpdateSupplier = isNeed;
    }


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "tea_ware_baseinfo_update_cache_.data";

    @JSONField(serialize = false)
    public static BaseInfoUpdateCache getFromCache() {
        BaseInfoUpdateCache data = (BaseInfoUpdateCache) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
            data = new BaseInfoUpdateCache();
        }
        return data;
    }

    @JSONField(serialize = false)
    public void save() {
        isForceUpdate =false;
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }

    public boolean isNeedUpdateJijiayuan() {
        return needUpdateJijiayuan;
    }

    public void setNeedUpdateJijiayuan(boolean needUpdateJijiayuan) {
        this.needUpdateJijiayuan = needUpdateJijiayuan;
    }

    public boolean isNeedUpdateSupplier() {
        return needUpdateSupplier;
    }

    public void setNeedUpdateSupplier(boolean needUpdateSupplier) {
        this.needUpdateSupplier = needUpdateSupplier;
    }

    public int getJijiayuanCount() {
        return jijiayuanCount;
    }

    public void setJijiayuanCount(int jijiayuanCount) {
        this.jijiayuanCount = jijiayuanCount;
    }

    public int getSupplierCount() {
        return supplierCount;
    }

    public void setSupplierCount(int supplierCount) {
        this.supplierCount = supplierCount;
    }

    public int getJijiayuanCountUpdated() {
        return jijiayuanCountUpdated;
    }

    public void setJijiayuanCountUpdated(int jijiayuanCountUpdated) {
        this.jijiayuanCountUpdated = jijiayuanCountUpdated;
    }

    public int getSupplierCountUpdated() {
        return supplierCountUpdated;
    }

    public void setSupplierCountUpdated(int supplierCountUpdated) {
        this.supplierCountUpdated = supplierCountUpdated;
    }

    public boolean isForceUpdate() {
        return isForceUpdate;
    }

    public void setForceUpdate(boolean forceUpdate) {
        isForceUpdate = forceUpdate;
    }

}
